<?php
// +----------------------------------------------------------------------
// | 青航系统 [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2023-永久 https://www.feifeiyu.club/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 青航-不吃芒果 <1716892803@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\exception\Jump;
use think\facade\View;
use think\facade\Cache;
use think\facade\Session;

class Login {
    
    
    
    public function index(){
        
        if (request()->isPost()) {
            $postField = 'name,password,verify';
			$data = request()->only(explode(',',$postField),'post',null);
    	    // 检查是否缓存
    		if(Cache::get(getip()))Jump::error("10分钟后再试!");
    		if(!$data['name'])Jump::error('账户名不能为空');
    		
    		if(!$data['password'])Jump::error("密码不能为空!");
    		if(!$data['verify'])Jump::error("验证码不能为空!");
            // 验证验证码
    		if(!captcha_check($data['verify'])) {
    			Jump::error('验证码错误!!');
    		};
    		
    		// 查询表
    		$arr = D('admin')->field('password,adminid,name,roleid,rolename')->where(['name'=>$data['name']])->find();
    		if(!$arr)Jump::error('用户名查询失败!');
    		
    		
            // 查询当天输错的密码记录
    		$lo_pas = D('admin_login_log')->where(['cause'=>2,'loginip'=> getip()])->whereTime('logintime', 'd')->count();
    		if($lo_pas == 2)Jump::error("密码输错两次!，三次以上后就10分钟后重试");
             // 超过次数设置缓存
    		if($lo_pas == 3) Cache::set(getip(),getip(),600);
    		
    		
    		
    		if($arr['password'] == password($data['password'])) {
    			$updata = ['logintime'=>time(),'loginip'=>getip()];
    			D('admin')->where(['name'=>$data['name']])->update($updata);
    			Session::set('adminid',$arr['adminid']);
    			Session::set('adminname',$arr['name']);
    			Session::set('roleid',$arr['roleid']);
    			Session::set('rolename',$arr['rolename']);
    			$log['logintime'] = time();
    			$log['loginip'] = getip();
    			$log['cause'] = 1;
    			$log['name'] = $data['name'];
    			$log['password'] = '';
                // 	密码正确设置本地缓存并记录
    			D('admin_login_log')->insert($log);
    			Jump::success("登录成功!",'/admin');
    			
    		}
    		
            // 记录用户输错密码
    		if($data['password'] != password($data['password'])) {
    			$log['logintime'] = time();
    			$log['loginip'] = getip();
    			$log['cause'] = 0;
    			$log['name'] = $data['name'];
    			$log['password'] = $data['password'];
                // 	密码错误记录
    			D('admin_login_log')->strict(false)->insert($log);
    			Jump::error('密码错误!!');
    		}
    		
            
    	}
    	
    	
        // 	检查本地是否有记录如果有则直接登录
    	if(Session::get('adminid'))Jump::success("登录成功!", url('admin/Index/index'));
    	return View::fetch();
    	
    }
    
    
    

    
}
